FrameLib  2.0
DSP processing with frames of arbitrary timing and length
FrameLib_Errors.h
Go to the documentation of this file.
1 
2 #ifndef FRAMELIB_ERRORS_H
3 #define FRAMELIB_ERRORS_H
4 
5 #include "FrameLib_Threading.h"
6 #include "FrameLib_Types.h"
7 
8 #include <algorithm>
9 #include <memory>
10 #include <string>
11 #include <string.h>
12 
20 // Possible sources of error
21 
23 
35 {
36 
37 public:
38 
47  struct HostNotifier : public FrameLib_Proxy
48  {
49  virtual void notify() = 0;
50  };
51 
61  {
63 
64  ErrorReport(ErrorSource source, FrameLib_Proxy *reporter, const char *error, const char *items, unsigned long numItems)
65  : mSource(source), mReporter(reporter), mError(error), mItems(items), mNumItems(numItems) {}
66 
67  public:
68 
69  ErrorReport() : mSource(kErrorObject), mReporter(nullptr), mError(nullptr), mItems(nullptr), mNumItems(0) {}
70 
71  void getErrorText(std::string& text) const;
72  ErrorSource getSource() const { return mSource; }
73  FrameLib_Proxy* getReporter() const { return mReporter; }
74 
75  private:
76 
77  // Data
78 
79  ErrorSource mSource;
80  FrameLib_Proxy* mReporter;
81  const char* mError;
82  const char* mItems;
83  unsigned long mNumItems;
84  };
85 
94  class ErrorList
95  {
97 
98  const static int sCharArraySize = 8192;
99  const static int sReportArraySize = 1024;
100 
101  public:
102 
103  public:
104 
114  {
115  friend ErrorList;
116 
117  ConstIterator(const ErrorReport *ptr) : mPtr(ptr) {};
118 
119  public:
120 
121  const ErrorReport operator *() { return *mPtr; }
122  const ErrorReport *operator ->() { return mPtr; }
123 
124  friend bool operator == (const ConstIterator &a, const ConstIterator &b) { return a.mPtr == b.mPtr; }
125  friend bool operator != (const ConstIterator &a, const ConstIterator &b) { return a.mPtr != b.mPtr; }
126 
127  ConstIterator& operator ++()
128  {
129  mPtr++;
130  return *this;
131  }
132 
133  ConstIterator operator ++(int)
134  {
135  ConstIterator result = *this;
136  operator++();
137  return result;
138  }
139 
140  private:
141 
142  // Pointer
143 
144  const ErrorReport *mPtr;
145  };
146 
147  ErrorList() : mReportsSize(0), mItemsSize(0), mFull(false) {}
148 
149  // Non-copyable
150 
151  ErrorList(const ErrorList&) = delete;
152  ErrorList& operator=(const ErrorList&) = delete;
153 
154  const ErrorReport operator[](size_t idx) const { return mReports[idx]; }
155  size_t size() const { return mReportsSize; }
156 
157  ConstIterator begin() const { return mReports; }
158  ConstIterator end() const { return mReports + mReportsSize; }
159 
160  bool isFull() const { return mFull; }
161 
162  private:
163 
164  bool addItem(const char *str)
165  {
166  char *ptr = mItems + mItemsSize;
167  size_t size = + strlen(str) + 1;
168 
169  if (mItemsSize + size < sCharArraySize)
170  {
171  strcpy(ptr, str);
172  mItemsSize += size;
173  return true;
174  }
175 
176  return false;
177  }
178 
179  bool addItem(long number)
180  {
181  char charArray[32];
182  sprintf(charArray, "%ld", number);
183  return addItem(charArray);
184  }
185 
186  bool addItem(double number)
187  {
188  char charArray[32];
189  sprintf(charArray, "%lf", number);
190  return addItem(charArray);
191  }
192 
193  bool addItems()
194  {
195  return true;
196  }
197 
198  template<typename T>
199  bool addItems(T first)
200  {
201  return addItem(first);
202  }
203 
204  template<typename T, typename... Args>
205  bool addItems(T first, Args... args)
206  {
207  if (addItem(first))
208  return addItems(args...);
209  return false;
210  }
211 
212  template<typename... Args>
213  void add(ErrorSource source, FrameLib_Proxy *reporter, const char *error, Args... args)
214  {
215  char *ptr = mItems + mItemsSize;
216 
217  if (!mFull && (mReportsSize < sReportArraySize) && addItems(args...))
218  {
219  mReports[mReportsSize] = ErrorReport(source, reporter, error, ptr, sizeof...(args));
220  mReportsSize++;
221  }
222  else
223  mFull = true;
224  }
225 
226  // Data
227 
228  ErrorReport mReports[sCharArraySize];
229  char mItems[sCharArraySize];
230  size_t mReportsSize;
231  size_t mItemsSize;
232  bool mFull;
233  };
234 
235  // Constructor
236 
237  FrameLib_ErrorReporter(HostNotifier *notifier) : mNotifier(notifier), mNotified (false), mReports(new ErrorList()) {}
238 
239  // Add and Retrieve Errors (list ownership is passed on retrieval)
240 
241  template<typename... Args>
242  void reportError(ErrorSource source, FrameLib_Proxy *reporter, const char *error, Args... args)
243  {
244  mReports->add(source, reporter, error, args...);
245 
246  if (mNotifier && !mNotified)
247  {
248  mNotifier->notify();
249  mNotified = true;
250  }
251  }
252 
253  std::unique_ptr<ErrorList> getErrors();
254 
255 private:
256 
257  // Data
258 
259  HostNotifier *mNotifier;
260  bool mNotified;
261  std::unique_ptr<ErrorList> mReports;
262  FrameLib_SpinLock mLock;
263 };
264 
265 #endif
ErrorReport()
Definition: FrameLib_Errors.h:69
std::unique_ptr< ErrorList > getErrors()
Definition: FrameLib_Errors.cpp:25
ConstIterator end() const
Definition: FrameLib_Errors.h:158
Definition: FrameLib_Errors.h:22
a spinlock that can be locked, attempted or acquired.
Definition: FrameLib_Threading.h:84
a virtual struct allowing for extensible communication to/from the host environment.
Definition: FrameLib_Types.h:69
ConstIterator begin() const
Definition: FrameLib_Errors.h:157
a report for a single error.
Definition: FrameLib_Errors.h:60
a class used to report errors to the host environment.
Definition: FrameLib_Errors.h:34
Definition: FrameLib_Errors.h:22
ErrorSource getSource() const
Definition: FrameLib_Errors.h:72
Definition: FrameLib_Errors.h:22
void reportError(ErrorSource source, FrameLib_Proxy *reporter, const char *error, Args... args)
Definition: FrameLib_Errors.h:242
bool isFull() const
Definition: FrameLib_Errors.h:160
a list of ErrorReport objects.
Definition: FrameLib_Errors.h:94
Definition: FrameLib_Errors.h:22
const ErrorReport operator[](size_t idx) const
Definition: FrameLib_Errors.h:154
size_t size() const
Definition: FrameLib_Errors.h:155
FL_SP operator*(const FL_SP &a, const FL_SP &b)
Definition: FrameLib_FixedPoint.cpp:126
FrameLib_Proxy * getReporter() const
Definition: FrameLib_Errors.h:73
an iterator for reports in the list (with an underlying const ErrorReport type).
Definition: FrameLib_Errors.h:113
a virtual struct used to supply a method for notifying the host of errors.
Definition: FrameLib_Errors.h:47
ErrorList()
Definition: FrameLib_Errors.h:147
ErrorSource
Definition: FrameLib_Errors.h:22
FrameLib_ErrorReporter(HostNotifier *notifier)
Definition: FrameLib_Errors.h:237